package com.zhike.b2b.util;

public class PageTool
{
  private int nowPage;
  private int pageCount;
  private String spath;
  StringBuffer toolString;

  public PageTool(int pageNo, int totalPage, String url)
  {
    nowPage = pageNo;
    pageCount = totalPage;
    spath = url;
    toolString = new StringBuffer();
  }

  public String getPageBar()
  {
    if (nowPage > pageCount)
      return "";
    if (nowPage <= 0)
      return "the param nowPage can't be negative number";
    if (spath.indexOf("{pageindex}") == -1)
      return "Eror Url";
    toolString.append(setCss());
    toolString.append("<div class='meneame'>");
    toolString.append(getPrePage());
    for (int i = 1; i <= pageCount; i++)
      toolString.append(getMiddlePage(i));
    toolString.append(getNextPage());
    toolString.append("共" + pageCount + "页");
    toolString.append(" 到第<input id='pageTool_pagenumber' style='text-align:center' class='pageinput' value='" + nowPage + "' size='3' />页");
    toolString.append("<input type='button' class='pageinput' value='确定' onclick='pageToolUrlOpen()'/>");
    toolString.append("<script type='text/javascript'>function pageToolUrlOpen(){var pageTool_url='" + spath + "';" + "var pageTool_maxPage=" + pageCount + ";" + "var pageTool_GoPage=document.getElementById('pageTool_pagenumber').value;" + "var pageTool_re = /^[0-9]*[1-9][0-9]*$/ ; " + "var pageTool_result = pageTool_re.test(pageTool_GoPage);" + "if(!pageTool_result)" + "{" + "\treturn false;" + "}" + "if(pageTool_GoPage<=0 || pageTool_GoPage>pageTool_maxPage)" + "{" + "\talert('总页数小于您输入的页数!');return false;" + "}" + "window.location.href=pageTool_url.replace('{pageindex}',pageTool_GoPage)" + "}" + "</script>");
    toolString.append("</div>");
    return toolString.toString();
  }

  public String getMiddlePage(int paramInt)
  {
    if (pageCount <= 6)
      return template(paramInt);
    if (paramInt <= 2)
      return template(paramInt);
    if ((paramInt - nowPage >= -2) && (paramInt - nowPage <= 4))
      return template(paramInt);
    if ((paramInt == 3) || (paramInt == pageCount))
    {
      if (!toolString.toString().endsWith("<span class='pagebreak'>...</span>"))
        return "<span class='pagebreak'>...</span>";
      return "";
    }
    return "";
  }

  public String getPrePage()
  {
    if (nowPage == 1)
      return "<span class='disabled'> <img src='/include/htmltag/images/arrow.gif' /> </span>";
    return "<a href='" + url(nowPage - 1) + "'><img src='/include/htmltag/images/arrow.gif' border='0'/></a>";
  }

  public String getNextPage()
  {
    if (nowPage == pageCount)
      return "<span class='disabled'>下一页</span>";
    return "<a href='" + url(nowPage + 1) + "'>下一页</a>";
  }

  public String getFirstPage()
  {
    if (nowPage == 1)
      return "<span class='disabled'>首页</span>";
    return "<a href='" + url(1) + "'>首页</a>";
  }

  public String getLastPage()
  {
    if (nowPage == pageCount)
      return "<span class='disabled'>尾页</span>";
    return "<a href='" + url(pageCount) + "'>尾页</a>";
  }

  public String url(int paramInt)
  {
    return spath.replace("{pageindex}", String.valueOf(paramInt));
  }

  public String setCss()
  {
    return "<link rel='stylesheet' href='/template/default/style/pagetool.css' type='text/css' />\n";
  }

  public String template(int paramInt)
  {
    if (nowPage == paramInt)
      return "<span class='current'>" + paramInt + "</span>";
    return "<a href='" + url(paramInt) + "'>" + paramInt + "</a>";
  }
  
  public String getPreNext()
  {
		String go = "";
		if(pageCount == 1){
			go = "<a class='pageUp'>上一页</a> | <a class='pageDown'>下一页</a>";
		}else if(nowPage==1){
			int next = nowPage+1;
			String nextpage = url(next);
			go = "<a class='pageUp'>上一页</a> | <a href='"+nextpage+"' class='pageDown'>下一页</a>";
		}else if(nowPage>1 && nowPage<pageCount){
			int last = nowPage-1;
			int next = nowPage+1;
			String lastpage = url(last);
			String nextpage = url(next);
			go = "<a href='"+lastpage+"' class='pageUp'>上一页</a> | <a href='"+nextpage+"' class='pageDown'>下一页</a>";
		}else if(nowPage==pageCount){
			int last = pageCount-1;
			String lastpage = url(last);
			go = "<a href='"+lastpage+"' class='pageUp'>上一页</a> | <a class='pageDown'>下一页</a>";
		}else{
			go = "<a class='pageUp'>上一页</a> | <a class='pageDown'>下一页</a>";
		}
		return go;
  }
}

